<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="JavaScript," />










<meta name="description" content="JS构建以及遍历二叉树1.二叉树的介绍二叉树 二叉树具有以下几个性质：  二叉树中，第 i 层最多有 2^i-1 个结点。 如果二叉树的深度为 K，那么此二叉树最多有  $2^K-1$  个结点。 二叉树中，终端结点数（叶子结点数）为 $n_0$，度为 2 的结点数为 $n_2$，则 $n_0&#x3D;n_2+1$。  每个结点最多只有两个度，如下图1，图二的A结点有三个度，因此不是二叉树：   满二叉树">
<meta property="og:type" content="article">
<meta property="og:title" content="JS构建以及遍历二叉树">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="JS构建以及遍历二叉树1.二叉树的介绍二叉树 二叉树具有以下几个性质：  二叉树中，第 i 层最多有 2^i-1 个结点。 如果二叉树的深度为 K，那么此二叉树最多有  $2^K-1$  个结点。 二叉树中，终端结点数（叶子结点数）为 $n_0$，度为 2 的结点数为 $n_2$，则 $n_0&#x3D;n_2+1$。  每个结点最多只有两个度，如下图1，图二的A结点有三个度，因此不是二叉树：   满二叉树">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/0.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/1.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/2.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/3.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/4.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/5.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/6.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/7.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/10.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/8.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/9.png">
<meta property="article:published_time" content="2022-07-13T14:13:01.000Z">
<meta property="article:modified_time" content="2022-07-14T12:23:44.165Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/0.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/07/13/JS构建以及遍历二叉树/"/>





  <title>JS构建以及遍历二叉树 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JS构建以及遍历二叉树</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-07-13T22:13:01+08:00">
                2022-07-13
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/07/13/JS构建以及遍历二叉树/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="JS构建以及遍历二叉树"><a href="#JS构建以及遍历二叉树" class="headerlink" title="JS构建以及遍历二叉树"></a>JS构建以及遍历二叉树</h1><h2 id="1-二叉树的介绍"><a href="#1-二叉树的介绍" class="headerlink" title="1.二叉树的介绍"></a>1.二叉树的介绍</h2><p><strong>二叉树</strong></p>
<p>二叉树具有以下几个性质：</p>
<ol>
<li>二叉树中，第 i 层最多有 2^i-1 个结点。</li>
<li>如果二叉树的深度为 K，那么此二叉树最多有  $2^K-1$  个结点。</li>
<li>二叉树中，终端结点数（叶子结点数）为 $n_0$，度为 2 的结点数为 $n_2$，则 $n_0=n_2+1$。</li>
</ol>
<p>每个结点最多只有两个度，如下图1，图二的A结点有三个度，因此不是二叉树：</p>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/0.png" class title="二叉树和非二叉树">

<p><strong>满二叉树</strong>除了满足普通二叉树的性质，还具有以下性质：</p>
<ol>
<li>满二叉树中第 i 层的节点数为 2n-1 个。</li>
<li>深度为 k 的满二叉树必有 2k-1 个节点 ，叶子数为 2k-1。</li>
<li>满二叉树中不存在度为 1 的节点，每一个分支点中都两棵深度相同的子树，且叶子节点都在最底层。</li>
<li>具有 n 个节点的满二叉树的深度为 log2(n+1)。</li>
</ol>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/1.png" class title="满二叉树和非满二叉树">

<p><strong>完全二叉树</strong>，如果二叉树中除去最后一层节点为满二叉树，且最后一层的结点依次从左到右分布，则此二叉树被称为完全二叉树</p>
<p>左侧是一棵完全二叉树，右侧由于最后一层的节点没有按照从左向右分布，因此只能算作是普通的二叉树。</p>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/2.png" class title="完全二叉树和非完全二叉树">

<span id="more"></span>

<h2 id="2-二叉树的遍历"><a href="#2-二叉树的遍历" class="headerlink" title="2.二叉树的遍历"></a>2.二叉树的遍历</h2><p>遍历一个二叉树有多种方法，这里笔者会讲述五种</p>
<ol>
<li>先序遍历</li>
<li>中序遍历</li>
<li>后序遍历</li>
<li>深度优先遍历</li>
<li>广度优先遍历</li>
</ol>
<h3 id="1-先序遍历"><a href="#1-先序遍历" class="headerlink" title="1.先序遍历"></a>1.先序遍历</h3><p>先序遍历也叫前序遍历，就是使用递归的方式，先遍历根节点，再遍历左子树，最后遍历右子树</p>
<p>对于如下所示的一棵二叉树</p>
<p>先序遍历的结果为：<strong>A B D G H C E I F</strong></p>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/3.png" class title="二叉树">

<p>过程如下：</p>
<ul>
<li>首先检查根节点，取出根节点的值<strong>A</strong>，根节点有左子树吗？有，往下走</li>
<li>遍历B结点，取出<strong>B</strong>，B结点有左子树吗？有，往下走</li>
<li>遍历D结点，取出<strong>D</strong>，D结点有左子树吗？有，往下走</li>
<li>遍历G结点，取出<strong>G</strong>，回溯遍历D结点有右子树吗？有，取出值<strong>H</strong>，H结点有左子树吗？没有。有右子树吗？没有，回溯</li>
<li>遍历B结点，有右子树吗？没有，回溯</li>
<li>遍历A结点，有右子树吗？有，遍历C结点，取出<strong>C</strong>，C有左子树吗？有，往下</li>
<li>遍历E结点，取出<strong>E</strong>，有左子树吗？没有。有右子树吗？有，遍历右子树I，取出<strong>I</strong>,I结点有左子树吗？没有。有右子树吗？没有，回溯</li>
<li>回溯遍历C结点，有右子树吗？有，取出<strong>F</strong>，F结点有左子树吗？没有。有右子树吗？没有，回溯发现所有节点都遍历完了，结束遍历。</li>
</ul>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/4.png" class title="先序遍历二叉树">

<p>因此先序(前序)遍历的结果为：A B D G H C E I F</p>
<h3 id="2-中序遍历"><a href="#2-中序遍历" class="headerlink" title="2.中序遍历"></a>2.中序遍历</h3><p>中序就是使用递归的方式，先遍历左子树，再遍历根节点，最后遍历右子树</p>
<p>对于如下所示的一棵二叉树</p>
<p>中序遍历的结果为：<strong>G D H B A E I C F</strong> </p>
<p>过程如下：</p>
<ul>
<li>首先检查根节点，根节点有左子树吗？有，往下走</li>
<li>检查B结点，B结点有左子树吗？有，往下走</li>
<li>检查D结点，D结点有左子树吗？有，往下走</li>
<li>检查G结点，D结点有左子树吗？没有，取出<strong>G</strong>，回溯遍历根结点D，取出值<strong>D</strong>，D结点有右子树吗？有，取出<strong>H</strong>，回溯</li>
<li>检查B结点，有右子树吗？没有，取出<strong>B</strong>，回溯</li>
<li>检查A结点，有右子树吗？有，取出<strong>A</strong>，往下走，</li>
<li>检查C结点，C有左子树吗？有，往下</li>
<li>检查E结点，E有左子树吗？没有，取出<strong>E</strong>，有右子树吗？有，检查右子树I，I有左子树吗？没有，取出<strong>I</strong>,I结点有左子树吗？没有。有右子树吗？没有，回溯</li>
<li>检查C结点，C的左子树遍历完毕，有右子树吗？有，取出<strong>C</strong>，检查F结点，E有左子树吗？没有。有右子树吗？没有，回溯发现所有节点都遍历完了，结束遍历。</li>
</ul>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/5.png" class title="中序遍历二叉树">

<p>得到中序遍历结果为：G D H B A E I C F </p>
<h3 id="3-后序遍历"><a href="#3-后序遍历" class="headerlink" title="3.后序遍历"></a>3.后序遍历</h3><p>后序就是使用递归的方式，先遍历左子树，再遍历右子树，最后遍历根节点</p>
<p>对于如下所示的一棵二叉树</p>
<p>中序遍历的结果为：<strong>G H D B  I E F C A</strong></p>
<p>过程如下：</p>
<ul>
<li>首先检查根节点，根节点有左子树吗？有，往下走</li>
<li>检查B结点，B结点有左子树吗？有，往下走</li>
<li>检查D结点，D结点有左子树吗？有，往下走</li>
<li>检查G结点，D结点有左子树吗？没有，取出<strong>G</strong>，回溯检查根结点D，D结点有右子树吗？有，取出值<strong>H</strong>，检查H结点有左子树吗？没有，H结点有右子树吗，没有，取出根节点<strong>D</strong>，回溯</li>
<li>检查B结点，有右子树吗？没有，取出<strong>B</strong>，回溯</li>
<li>检查A结点，有右子树吗？有，往下走，</li>
<li>检查C结点，C有左子树吗？有，往下</li>
<li>检查E结点，E有左子树吗？没有，有右子树吗？有，取出<strong>I</strong>，检查结点I，I有左子树吗？没有,I结点有右子树吗？没有。取出<strong>E</strong>，回溯</li>
<li>检查C结点，C的左子树遍历完毕，有右子树吗？有，检查F结点，F有左子树吗？没有。有右子树吗？没有，取出<strong>F</strong>，回溯取出<strong>C</strong>，发现所有节点都遍历完了，结束遍历。</li>
</ul>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/6.png" class title="中序遍历二叉树">

<p>得到中序遍历结果为：G D H B A E I C F </p>
<h3 id="4-深度优先遍历"><a href="#4-深度优先遍历" class="headerlink" title="4.深度优先遍历"></a>4.深度优先遍历</h3><ul>
<li></li>
</ul>
<h3 id="5-广度优先遍历"><a href="#5-广度优先遍历" class="headerlink" title="5.广度优先遍历"></a>5.广度优先遍历</h3><ul>
<li></li>
</ul>
<h2 id="3-根据先序遍历构造一棵二叉树"><a href="#3-根据先序遍历构造一棵二叉树" class="headerlink" title="3.根据先序遍历构造一棵二叉树"></a>3.根据先序遍历构造一棵二叉树</h2><p>读者以完全满二叉树来创建，如果为空的叶子结点，则用#代替，使用递归创建</p>
<p>先序创建这样一棵二叉树</p>
<p><strong>A B D G  # # H # # # C E # I # # F # #</strong></p>
<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/7.png" class title="后序遍历二叉树">

<h3 id="1-定义叶子结点"><a href="#1-定义叶子结点" class="headerlink" title="1.定义叶子结点"></a>1.定义叶子结点</h3><p>包含3个部分</p>
<ul>
<li>数据域</li>
<li>左孩子指针</li>
<li>右孩子指针</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@name</span>:binaryTree</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>:louchao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>:2022/7/12 18:58</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">let</span> strArr = <span class="string">&#x27;ABDG##H###CE#I##F##&#x27;</span>.split(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TreeNode</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">element</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = element;</span><br><span class="line">        <span class="built_in">this</span>.lChild = <span class="literal">null</span>;</span><br><span class="line">        <span class="built_in">this</span>.rChild = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-构造二叉树"><a href="#2-构造二叉树" class="headerlink" title="2.构造二叉树"></a>2.构造二叉树</h3><p>递归构造</p>
<p>如果是#则构造叶子结点，否则构造子树</p>
<p>先确定根节点，再构造左子树，最后构造右子树</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 先序创建二叉树</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;null&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">createBiTree</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (strArr.length===<span class="number">0</span>) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">let</span> str = strArr.shift();</span><br><span class="line">    <span class="keyword">if</span> (str === <span class="string">&#x27;#&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">    biTree.data = str;</span><br><span class="line">    <span class="keyword">if</span> (strArr[<span class="number">0</span>]!==<span class="string">&#x27;#&#x27;</span>)&#123;   <span class="comment">//如果下一个数不为空，则创建左子树</span></span><br><span class="line">        biTree.lChild = <span class="keyword">new</span> TreeNode(<span class="string">&#x27;#&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">this</span>.createBiTree(biTree.lChild);   <span class="comment">//直到 str === &#x27;#&#x27; 推出这次递归，进入下列递归</span></span><br><span class="line">    <span class="keyword">if</span> (strArr[<span class="number">0</span>]!==<span class="string">&#x27;#&#x27;</span>)&#123;   <span class="comment">//如果下一个数不为空，则创建右子树</span></span><br><span class="line">        biTree.rChild = <span class="keyword">new</span> TreeNode(<span class="string">&#x27;#&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">this</span>.createBiTree(biTree.rChild);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/10.png" class title="先序遍历构造二叉树结果">

<h3 id="3-先序遍历"><a href="#3-先序遍历" class="headerlink" title="3.先序遍历"></a>3.先序遍历</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 先序遍历</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">preOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (biTree === <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">    <span class="built_in">this</span>.preOrderTraverse(biTree.lChild);</span><br><span class="line">    <span class="built_in">this</span>.preOrderTraverse(biTree.rChild)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-中序遍历"><a href="#4-中序遍历" class="headerlink" title="4.中序遍历"></a>4.中序遍历</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 中序遍历</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">inOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (biTree === <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">this</span>.inOrderTraverse(biTree.lChild);</span><br><span class="line">    <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">    <span class="built_in">this</span>.inOrderTraverse(biTree.rChild);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-后序遍历"><a href="#5-后序遍历" class="headerlink" title="5.后序遍历"></a>5.后序遍历</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后序遍历</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">postOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (biTree===<span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">this</span>.postOrderTraverse(biTree.lChild);</span><br><span class="line">    <span class="built_in">this</span>.postOrderTraverse(biTree.rChild);</span><br><span class="line">    <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>测试：</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> bt = <span class="keyword">new</span> BinaryTree();</span><br><span class="line">bt.createBiTree(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 先序遍历 =================&quot;</span>);</span><br><span class="line">bt.preOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 中序遍历 =================&quot;</span>);</span><br><span class="line">bt.inOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 后序遍历 =================&quot;</span>);</span><br><span class="line">bt.postOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;==========================================&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.dir(newTree);</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/8.png" class title="测试结果">

<img src="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/9.png" class title="测试结果">

<h2 id="4-附所有代码"><a href="#4-附所有代码" class="headerlink" title="4.附所有代码"></a>4.附所有代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@name</span>:binaryTree</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>:louchao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>:2022/7/12 18:58</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">let</span> strArr = <span class="string">&#x27;ABDG##H###CE#I##F##&#x27;</span>.split(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TreeNode</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">element</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = element;</span><br><span class="line">        <span class="built_in">this</span>.lChild = <span class="literal">null</span>;</span><br><span class="line">        <span class="built_in">this</span>.rChild = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> newTree = <span class="keyword">new</span> TreeNode(<span class="string">&quot;#&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BinaryTree</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 先序创建二叉树</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;null&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">createBiTree</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (strArr.length===<span class="number">0</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">let</span> str = strArr.shift();</span><br><span class="line">        <span class="keyword">if</span> (str === <span class="string">&#x27;#&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">        biTree.data = str;</span><br><span class="line">        <span class="keyword">if</span> (strArr[<span class="number">0</span>]!==<span class="string">&#x27;#&#x27;</span>)&#123;   <span class="comment">//如果下一个数不为空，则创建左子树</span></span><br><span class="line">            biTree.lChild = <span class="keyword">new</span> TreeNode(<span class="string">&#x27;#&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.createBiTree(biTree.lChild);   <span class="comment">//直到 str === &#x27;#&#x27; 推出这次递归，进入下列递归</span></span><br><span class="line">        <span class="keyword">if</span> (strArr[<span class="number">0</span>]!==<span class="string">&#x27;#&#x27;</span>)&#123;   <span class="comment">//如果下一个数不为空，则创建右子树</span></span><br><span class="line">            biTree.rChild = <span class="keyword">new</span> TreeNode(<span class="string">&#x27;#&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.createBiTree(biTree.rChild);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 先序遍历</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">preOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (biTree === <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">        <span class="built_in">this</span>.preOrderTraverse(biTree.lChild);</span><br><span class="line">        <span class="built_in">this</span>.preOrderTraverse(biTree.rChild)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 中序遍历</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">inOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (biTree === <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="built_in">this</span>.inOrderTraverse(biTree.lChild);</span><br><span class="line">        <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">        <span class="built_in">this</span>.inOrderTraverse(biTree.rChild);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 后序遍历</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">biTree</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">postOrderTraverse</span>(<span class="params">biTree</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (biTree===<span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="built_in">this</span>.postOrderTraverse(biTree.lChild);</span><br><span class="line">        <span class="built_in">this</span>.postOrderTraverse(biTree.rChild);</span><br><span class="line">        <span class="built_in">console</span>.log(biTree.data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> bt = <span class="keyword">new</span> BinaryTree();</span><br><span class="line">bt.createBiTree(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 先序遍历 =================&quot;</span>);</span><br><span class="line">bt.preOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 中序遍历 =================&quot;</span>);</span><br><span class="line">bt.inOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;================ 后序遍历 =================&quot;</span>);</span><br><span class="line">bt.postOrderTraverse(newTree);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;==========================================&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.dir(newTree);</span><br></pre></td></tr></table></figure>

<p><strong>深度优先遍历和广度优先遍历改天更新，今天太晚了===============</strong></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/" rel="next" title="JS实现队列">
                <i class="fa fa-chevron-left"></i> JS实现队列
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91"><span class="nav-number">1.</span> <span class="nav-text">JS构建以及遍历二叉树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">1.二叉树的介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.</span> <span class="nav-text">2.二叉树的遍历</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.1.</span> <span class="nav-text">1.先序遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.中序遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.3.</span> <span class="nav-text">3.后序遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.4.</span> <span class="nav-text">4.深度优先遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.5.</span> <span class="nav-text">5.广度优先遍历</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%A0%B9%E6%8D%AE%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86%E6%9E%84%E9%80%A0%E4%B8%80%E6%A3%B5%E4%BA%8C%E5%8F%89%E6%A0%91"><span class="nav-number">1.3.</span> <span class="nav-text">3.根据先序遍历构造一棵二叉树</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E5%8F%B6%E5%AD%90%E7%BB%93%E7%82%B9"><span class="nav-number">1.3.1.</span> <span class="nav-text">1.定义叶子结点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91"><span class="nav-number">1.3.2.</span> <span class="nav-text">2.构造二叉树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.3.3.</span> <span class="nav-text">3.先序遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.3.4.</span> <span class="nav-text">4.中序遍历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="nav-number">1.3.5.</span> <span class="nav-text">5.后序遍历</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E9%99%84%E6%89%80%E6%9C%89%E4%BB%A3%E7%A0%81"><span class="nav-number">1.4.</span> <span class="nav-text">4.附所有代码</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/';
          this.page.identifier = '2022/07/13/JS构建以及遍历二叉树/';
          this.page.title = 'JS构建以及遍历二叉树';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
